/*
 * FNISVGEditor - JavaScript - based editor for SVG graphics 
 * Copyright (C) 2008 - 2009 Antti Leppä / Foyt
 * http://www.foyt.fi
 * 
 * License: 
 * 
 * Licensed under GNU Lesser General Public License Version 2.1 or later (the "LGPL") 
 * http://www.gnu.org/licenses/lgpl.html
 */
FNIConfigurationManager = Class.create({
  initialize: function(configuration){
    this._values = new Hash();
    
    var defConf = FNIDefaultSVGEditorConfiguration;
    
    for (var i in defConf) 
      this._values.set(i, defConf[i]);
    
    if (configuration) 
      this._values = this._mergeHash(this._values, configuration);    
  },
  getValue: function (path) {
    var keys = path.split("/");
    
    var c = this._values;
    
    for (var i = 0; i < keys.length; i++) {
      var key = keys[i];
      if (Object.isHash(c))
        c = c.get(key);
      else 
        c = c[key];
    }
    
    return c;
  },
  setValue: function(key, value){
    this._values.set(key, value);
  },
  _mergeHash: function(conf, hash) {
    var value = $H(hash);
    
    var keys = value.keys();
    
    for (var i = 0; i < keys.length; i++) {
      var key = keys[i]; 
      
      var v = value.get(key);
      var o = conf.get(key);
      if (o) { 
        if (this._isMap(v) == true) {
          conf.set(key, this._mergeHash($H(o), v));
        } else {
          conf.set(key, v);          
        }
      } else {
        conf.set(key, v);
      }
    }
    
    return conf;
  },
  _isMap: function (v) {
    if (Object.isArray(v) || Object.isNumber(v) || Object.isString(v))
      return false;
    else 
      return true;
  }
});
